﻿namespace Vdc.Grid
{
    using Vdc.Interface;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    internal class GridBody : WebControl, INamingContainer
    {
        private Table container;
        private int customDataRowsNumber;
        private GridRow dataRow;
        internal List<GridRow> dataRows;
        private HtmlContainer detailsExpander;
        private bool detailsExpanderAdded;
        private Vdc.Grid.Grid grid;
        private HtmlContainer innerBody;
        public HtmlContainer loadingMessageContainer;
        private Hashtable previouslyGroupedValues;
        private string recordStyleDisplay;
        private int rowIndex;

        public GridBody(Vdc.Grid.Grid _grid)
        {
            this.grid = _grid;
            this.ID = this.GetBodyID();
            this.dataRows = new List<GridRow>();
            this.customDataRowsNumber = 0;
            if (this.grid.AllowGrouping)
            {
                this.previouslyGroupedValues = new Hashtable();
            }
        }

        protected virtual void BindCellTemplate(GridDataControlFieldCell cell, string dataValue)
        {
            Column containingField = cell.ContainingField as Column;
            GridRow parent = (GridRow) cell.Parent;
            HtmlContainer container = cell.Controls[0].Controls[0] as HtmlContainer;
            if (container.Template != null)
            {
                container.Template.Container.Value = dataValue;
                container.Template.Container.DataObject = (this.grid.RowObjects.Count > parent.DataItemIndex) ? this.grid.RowObjects[parent.DataItemIndex] : null;
                container.Template.Container.DataItem = parent.ToHashtable();
                container.Template.Container.PageRecordIndex = parent.RowIndex;
                container.Template.Container.RecordIndex = parent.DataItemIndex;
                container.Template.Container.PageDataRecordIndex = parent.RowIndex - this.customDataRowsNumber;
                container.Template.Container.DataRecordIndex = parent.DataItemIndex - this.customDataRowsNumber;
                container.Template.Container.ColumnIndex = containingField.Index;
                container.Template.Container.GridClientID = this.grid.GridID;
                container.Template.DataBind();
            }
            else if (container.RuntimeTemplate != null)
            {
                container.RuntimeTemplate.Container.Value = dataValue;
                container.RuntimeTemplate.Container.DataObject = (this.grid.RowObjects.Count > parent.DataItemIndex) ? this.grid.RowObjects[parent.DataItemIndex] : null;
                container.RuntimeTemplate.Container.DataItem = parent.ToHashtable();
                container.RuntimeTemplate.Container.PageRecordIndex = parent.RowIndex;
                container.RuntimeTemplate.Container.RecordIndex = parent.DataItemIndex;
                container.RuntimeTemplate.Container.PageDataRecordIndex = parent.RowIndex - this.customDataRowsNumber;
                container.RuntimeTemplate.Container.DataRecordIndex = parent.DataItemIndex - this.customDataRowsNumber;
                container.RuntimeTemplate.Container.ColumnIndex = containingField.Index;
                container.RuntimeTemplate.Container.GridClientID = this.grid.GridID;
                container.RuntimeTemplate.DataBind();
            }
        }

        protected virtual void CreateActionLinks(HtmlContainer container, Column field)
        {
            HyperLink child = null;
            HyperLink link2 = null;
            if (field.AllowEdit)
            {
                child = new HyperLink();
                child.Attributes["href"] = "javascript: //";
                child.Attributes["onclick"] = GridEncodingHelper.XMLCdataEncode(this.grid.GridID + ".editRecord(this)") + ";return false;";
                child.Text = GridEncodingHelper.XMLCdataEncode(this.grid.LocalizationSettings.EditLink);
                child.CssClass = this.grid.CssSettings.CSSActionLink;
            }
            if (field.AllowDelete)
            {
                link2 = new HyperLink();
                link2.Attributes["href"] = "javascript: //";
                link2.Attributes["onclick"] = GridEncodingHelper.XMLCdataEncode(this.grid.GridID + ".deleteRow(this)") + ";return false;";
                link2.Text = GridEncodingHelper.XMLCdataEncode(this.grid.LocalizationSettings.DeleteLink);
                link2.CssClass = this.grid.CssSettings.CSSActionLink;
            }
            if (field.AllowEdit || field.AllowDelete)
            {
                if (field.AllowEdit)
                {
                    container.Controls.Add(child);
                }
                if (field.AllowEdit && field.AllowDelete)
                {
                    Literal literal = new Literal {
                        Text = " | "
                    };
                    container.Controls.Add(literal);
                }
                if (field.AllowDelete)
                {
                    container.Controls.Add(link2);
                }
            }
            else if (this.grid.IsDesignTimeRender)
            {
                container.Text = "&nbsp;";
            }
        }

        protected virtual void CreateBodyGroupHeaderRow(object dataItem)
        {
            GridRow row = new GridRow(-1, -1, GridRowType.DataRow, DataControlRowState.Normal) {
                DataItem = dataItem
            };
            Hashtable hash = row.ToHashtable();
            string str = "";
            string str2 = "";
            if ((this.grid.AllowGrouping && !string.IsNullOrEmpty(this.grid.GroupBy)) && this.grid.ShowCollapsedGroups)
            {
                this.recordStyleDisplay = "none";
                if (this.grid.IsDesignTimeRender && this.grid.UseEmbeddedImages)
                {
                    str2 = "url(" + this.grid.ImagesURL.Grouping_RecordGroupHeader + ")";
                }
                else
                {
                    str = "none";
                }
            }
            else if (this.grid.IsDesignTimeRender && this.grid.UseEmbeddedImages)
            {
                str2 = "url(" + this.grid.ImagesURL.Grouping_RecordGroupHeader + ")";
            }
            bool flag = false;
            if (this.grid.ShowGroupFooter)
            {
                for (int j = this.grid.ArrGroupByColumns.Count - 1; j >= 0; j--)
                {
                    if ((this.previouslyGroupedValues[this.grid.ArrGroupByColumns[j].ToString()] != null) && (hash[this.grid.ArrGroupByColumns[j].ToString()].ToString() != this.previouslyGroupedValues[this.grid.ArrGroupByColumns[j].ToString()].ToString()))
                    {
                        this.CreateGroupFooter(j);
                    }
                }
            }
            for (int i = 0; i < this.grid.ArrGroupByColumns.Count; i++)
            {
                if (((this.grid.GetHashValueByDataField(this.previouslyGroupedValues, this.grid.ArrGroupByColumns[i].ToString()) == null) || (this.grid.GetHashValueByDataField(hash, this.grid.ArrGroupByColumns[i].ToString()) != this.grid.GetHashValueByDataField(this.previouslyGroupedValues, this.grid.ArrGroupByColumns[i].ToString()))) || flag)
                {
                    if ((!this.grid.IsDesignTimeRender || !this.grid.ShowCollapsedGroups) || (i <= 0))
                    {
                        GridRow row2 = new GridRow(this.rowIndex, this.rowIndex, GridRowType.GroupHeader, DataControlRowState.Normal) {
                            CssClass = this.grid.CssSettings.CSSRecordGroupHeader,
                            TableSection = TableRowSection.TableBody
                        };
                        bool flag2 = false;
                        if (this.grid.IsDesignTimeRender || (i > 0))
                        {
                            if (!string.IsNullOrEmpty(str))
                            {
                                row2.Style["display"] = str;
                            }
                            if (!string.IsNullOrEmpty(str2))
                            {
                                row2.Style["background-image"] = str2;
                            }
                        }
                        TableCell child = new TableCell {
                            ColumnSpan = this.grid.GetNumberOfVisibleColumns()
                        };
                        row2.Controls.Add(child);
                        HtmlContainer container = new HtmlContainer {
                            CssClass = "ob_gRGHI",
                            Text = "&#160;"
                        };
                        child.Controls.Add(container);
                        for (int k = 0; k < i; k++)
                        {
                            HtmlContainer container2 = new HtmlContainer {
                                CssClass = "ob_gRGBS"
                            };
                            if (k == (i - 1))
                            {
                                HtmlContainer container3 = new HtmlContainer {
                                    Text = "&#160;"
                                };
                                container2.Controls.Add(container3);
                            }
                            else
                            {
                                container2.Text = "&#160;";
                            }
                            container.Controls.Add(container2);
                        }
                        HtmlContainer container4 = new HtmlContainer {
                            CssClass = this.grid.CssSettings.CSSRecordGroupHeaderButton
                        };
                        Image image = new Image {
                            ImageUrl = !this.grid.ShowCollapsedGroups ? this.grid.ImagesURL.Grouping_BtnClose : this.grid.ImagesURL.Grouping_BtnOpen
                        };
                        image.Attributes["alt"] = "";
                        image.Attributes["onclick"] = this.grid.GridID + ".manageGroupExpandCollapse(this,false)";
                        container4.Controls.Add(image);
                        container.Controls.Add(container4);
                        Column columnByDataField = this.grid.Columns.GetColumnByDataField(this.grid.ArrGroupByColumns[i].ToString());
                        string multiPageGroupsInformation = "";
                        int[] numArray = null;
                        int index = (this.rowIndex + (this.grid.CurrentPageIndex * this.grid.GetPageSize(true))) - this.customDataRowsNumber;
                        if (((string.IsNullOrEmpty(this.grid.TemplateSettings.GroupHeaderTemplateId) && (i == 0)) && (this.grid.ShowGroupsInfo || this.grid.ShowMultiPageGroupsInfo)) && !this.grid.IsDesignTimeRender)
                        {
                            multiPageGroupsInformation = this.grid.GetMultiPageGroupsInformation(index, columnByDataField.DataField);
                        }
                        else if (!string.IsNullOrEmpty(this.grid.TemplateSettings.GroupHeaderTemplateId))
                        {
                            numArray = new int[4];
                            numArray = this.grid.CheckMultiPageGroupOfRecords(index, columnByDataField.DataField);
                        }
                        HtmlContainer container5 = new HtmlContainer {
                            CssClass = this.grid.CssSettings.CSSRecordGroupHeaderCell
                        };
                        container5.Attributes["onclick"] = this.grid.GridID + ".manageGroupExpandCollapse(this,true)";
                        container.Controls.Add(container5);
                        if (this.grid.GetHashValueByDataField(hash, this.grid.ArrGroupByColumns[i].ToString()) != null)
                        {
                            Column column2 = this.grid.Columns.GetColumnByDataField(this.grid.ArrGroupByColumns[i].ToString());
                            object cellData = row.GetCellData(column2.DataField);
                            if ((column2 != null) && !string.IsNullOrEmpty(column2.DataFormatString_GroupHeader))
                            {
                                cellData = string.Format(CultureInfo.CurrentCulture, column2.DataFormatString_GroupHeader, new object[] { cellData });
                            }
                            string text = cellData.ToString();
                            string sValue = "";
                            if (!columnByDataField.ParseHTML)
                            {
                                sValue = GridEncodingHelper.XMLCdataEncode(text).Replace("\r\n", "<br />");
                            }
                            else
                            {
                                sValue = text.Replace("\r\n", "<br />");
                            }
                            if (string.IsNullOrEmpty(this.grid.TemplateSettings.GroupHeaderTemplateId))
                            {
                                Literal literal = new Literal();
                                container5.Controls.Add(literal);
                                string str6 = columnByDataField.HeaderText + ": ";
                                if (string.IsNullOrEmpty(columnByDataField.TemplateSettings.GroupHeaderTemplateId))
                                {
                                    str6 = str6 + sValue;
                                }
                                else
                                {
                                    GridTemplate template = this.grid.CreateTemplate(columnByDataField.TemplateSettings.GroupHeaderTemplateId, sValue, null, null, 0, 0);
                                    container5.Controls.Add(template);
                                    flag2 = true;
                                }
                                literal.Text = str6;
                            }
                            else
                            {
                                GridGroupInformation groupInfo = new GridGroupInformation {
                                    TotalRecordsCount = numArray[1],
                                    PageRecordsCount = numArray[0],
                                    HasRecordsOnPreviousPage = numArray[2] != 0,
                                    HasRecordsOnNextPage = numArray[3] != 0
                                };
                                GridTemplate template2 = this.grid.CreateTemplate(this.grid.TemplateSettings.GroupHeaderTemplateId, sValue, columnByDataField, groupInfo);
                                container5.Controls.Add(template2);
                                flag2 = true;
                            }
                        }
                        if (i == 0)
                        {
                            Literal literal2 = new Literal {
                                Text = multiPageGroupsInformation
                            };
                            container5.Controls.Add(literal2);
                            this.previouslyGroupedValues = new Hashtable();
                        }
                        row2.GroupLevel = i;
                        GridRowEventArgs e = new GridRowEventArgs(row2);
                        this.grid.OnRowCreated(e);
                        this.container.Rows.Add(row2);
                        this.rowIndex++;
                        this.customDataRowsNumber++;
                        if (flag2)
                        {
                            row2.DataBind();
                        }
                        if (this.grid.internalDataBinding)
                        {
                            this.grid.OnRowDataBound(e);
                        }
                        flag = true;
                    }
                    if (this.grid.GetHashValueByDataField(hash, this.grid.ArrGroupByColumns[i].ToString()) != null)
                    {
                        this.grid.SetHashValueByDataField(this.previouslyGroupedValues, this.grid.ArrGroupByColumns[i].ToString(), this.grid.GetHashValueByDataField(hash, this.grid.ArrGroupByColumns[i].ToString()));
                    }
                }
                else
                {
                    GridRow row3 = new GridRow(-1, -1, GridRowType.GroupHeader, DataControlRowState.Normal);
                    this.container.Rows.Add(row3);
                    row3.Visible = false;
                }
            }
        }

        protected virtual GridDataControlFieldCell CreateCell(Column field)
        {
            GridDataControlFieldCell child = new GridDataControlFieldCell(field);
            if (field.Visible)
            {
                child.CssClass = !field.Wrap ? "ob_gC" : "ob_gCW";
            }
            else
            {
                child.CssClass = !field.Wrap ? "ob_gC_I" : "ob_gCW_I";
            }
            if (field.Index == this.grid.IndexOfFirstVisibleColumn)
            {
                child.CssClass = child.CssClass + " ob_gC_Fc";
            }
            else if (field.Index == this.grid.IndexOfLastVisibleColumn)
            {
                child.CssClass = child.CssClass + " ob_gC_Lc";
            }
            if (field.Width.IndexOf("%") == -1)
            {
                child.Width = Unit.Pixel(int.Parse(field.Width));
            }
            else
            {
                child.Width = Unit.Percentage((double) int.Parse(field.Width.Replace("%", "")));
            }
            if (this.grid.IsDesignTimeRender)
            {
                if (!field.Visible)
                {
                    child.Style["display"] = "none";
                    child.Width = Unit.Pixel(0);
                }
                child.Attributes["_designerRegion"] = field.Index.ToString();
            }
            HtmlContainer container = new HtmlContainer();
            HtmlContainer container2 = new HtmlContainer();
            container.CssClass = "ob_gCc1";
            if (string.IsNullOrEmpty(field.Align) || (field.Align.ToLower() == "left"))
            {
                container2.CssClass = "ob_gCc2";
            }
            else if (field.Align.ToLower() == "center")
            {
                container2.CssClass = "ob_gCc2C";
            }
            else
            {
                container2.CssClass = "ob_gCc2R";
            }
            container.Controls.Add(container2);
            child.Controls.Add(container);
            this.dataRow.Controls.Add(child);
            bool flag = false;
            if (((this.grid.DetailGrids.Count > 0) && !this.detailsExpanderAdded) && field.Visible)
            {
                this.CreateExpandCollapseButtonContainer(child);
                this.detailsExpanderAdded = true;
                flag = true;
            }
            child.DataBinding += new System.EventHandler(this.PopulateCell);
            if (((!field.AllowEdit && !field.AllowDelete) && !string.IsNullOrEmpty(field.DataField)) && ((!string.IsNullOrEmpty(field.DataFormatString) && !field.ApplyFormatInEditMode) || (field.ParseHTML || !string.IsNullOrEmpty(field.TemplateSettings.TemplateId))))
            {
                flag = true;
            }
            if (!string.IsNullOrEmpty(field.TemplateSettings.TemplateId))
            {
                this.CreateCellTemplate(container2, field.TemplateSettings.TemplateId);
            }
            else if (field.AllowEdit || field.AllowDelete)
            {
                this.CreateActionLinks(container2, field);
            }
            if (flag || (field is CheckBoxColumn))
            {
                HtmlContainer container3 = new HtmlContainer {
                    CssClass = "ob_gCd"
                };
                container.Controls.Add(container3);
            }
            return child;
        }

        protected virtual void CreateCellSeparator(string width, bool visible)
        {
            HtmlContainer child = new HtmlContainer {
                Text = "&#160;",
                CssClass = "ob_gCS"
            };
            child.Style["left"] = width;
            if (!visible || this.grid.HasColumnsWithFixedAndPercentageWidth)
            {
                child.Style["display"] = "none";
            }
            this.innerBody.Controls.Add(child);
        }

        protected virtual void CreateCellTemplate(HtmlContainer container, string templateId)
        {
            GridTemplate child = null;
            GridRuntimeTemplate template2 = null;
            if (this.grid.Templates.GetById(templateId) is GridRuntimeTemplate)
            {
                GridRuntimeTemplate runtimeTemplateById = this.grid.Templates.GetRuntimeTemplateById(templateId);
                if (runtimeTemplateById == null)
                {
                    throw new InvalidTemplateIDException(templateId);
                }
                template2 = (GridRuntimeTemplate) runtimeTemplateById.Clone();
            }
            else
            {
                GridTemplate byId = this.grid.Templates.GetById(templateId);
                if (byId == null)
                {
                    throw new InvalidTemplateIDException(templateId);
                }
                child = (GridTemplate) byId.Clone();
            }
            if (child != null)
            {
                container.Template = child;
                container.Controls.Add(child);
            }
            else if (template2 != null)
            {
                container.RuntimeTemplate = template2;
                container.Controls.Add(template2);
            }
        }

        protected override void CreateChildControls()
        {
            this.CssClass = this.grid.CssSettings.CSSBodyContainer;
            if (this.grid.ScrollingSettings.ScrollHeight != Unit.Pixel(0))
            {
                if (this.grid.ScrollingSettings.ScrollHeight.ToString().IndexOf("%") == -1)
                {
                    base.Style["max-height"] = this.grid.ScrollingSettings.ScrollHeight.ToString();
                }
                else
                {
                    base.Style["height"] = "100%";
                }
            }
            if ((this.grid.ScrollingSettings.ScrollHeight == Unit.Pixel(0)) && (this.grid.Height == Unit.Pixel(0)))
            {
                base.Style["overflow-y"] = "hidden";
            }
            if (this.grid.Height != Unit.Pixel(0))
            {
                base.Style["height"] = ((this.grid.Height.Value / 2.0)).ToString() + "px";
            }
            this.innerBody = new HtmlContainer();
            this.innerBody.CssClass = "ob_gBICont";
            if ((this.grid.ScrollingSettings.ScrollWidth != 0) && (this.grid.ScrollingSettings.NumberOfFixedColumns != 0))
            {
                GridFixedColumnsPositionType fixedColumnsPosition = this.grid.ScrollingSettings.FixedColumnsPosition;
            }
            this.Controls.Add(this.innerBody);
            this.container = new Table();
            this.container.CssClass = this.grid.CssSettings.CSSBody;
            this.container.Attributes["cellspacing"] = "0";
            this.container.Attributes["cellpadding"] = "0";
            this.container.Width = Unit.Percentage(100.0);
            this.innerBody.Controls.Add(this.container);
            this.CreateTableLayoutRow();
            if (!this.grid.ScrollingSettings.EnableVirtualScrolling || !this.grid._isVirtualScrolling)
            {
                this.rowIndex = 0;
            }
            else
            {
                this.rowIndex = this.grid.CurrentPageIndex * this.grid.PageSize;
            }
            if (this.grid.pagedDataSource.Count > 0)
            {
                foreach (object obj2 in this.grid.pagedDataSource)
                {
                    if (this.grid.AllowGrouping && (this.grid.GroupBy != string.Empty))
                    {
                        this.CreateBodyGroupHeaderRow(obj2);
                    }
                    this.CreateDataRow(obj2);
                    if ((this.grid.ShowGroupFooter && !string.IsNullOrEmpty(this.grid.GroupBy)) && (this.rowIndex == ((this.grid.pagedDataSource.Count - 1) + this.customDataRowsNumber)))
                    {
                        for (int i = this.grid.ArrGroupByColumns.Count - 1; i >= 0; i--)
                        {
                            this.CreateGroupFooter(i);
                        }
                    }
                    if ((this.grid.DetailGrids.Count > 0) && this.CreateDetailGrid())
                    {
                        break;
                    }
                    this.rowIndex++;
                }
            }
            if (this.dataRows.Count == 0)
            {
                this.CreateEmptyDataRow();
            }
            if (this.grid.SelectedRecordsIndexesContainer != null)
            {
                this.grid.SelectedRecordsIndexesContainer.Value = this.grid.selectedRowsIndexes;
            }
            int num2 = 0;
            int num3 = 0;
            foreach (Column column in this.grid.Columns)
            {
                bool visible = column.Visible;
                if (num2 < (this.grid.Columns.Count - 1))
                {
                    if (column.Width.IndexOf("%") == -1)
                    {
                        this.CreateCellSeparator(((num3 + int.Parse(column.Width))).ToString() + "px", visible);
                    }
                    else
                    {
                        this.CreateCellSeparator(((num3 + int.Parse(column.Width.Replace("%", "")))).ToString() + "%", visible);
                    }
                }
                if (column.Visible)
                {
                    num3 += int.Parse(column.Width.Replace("%", ""));
                }
                num2++;
            }
            if (this.grid.CallbackMode && this.grid.ShowLoadingMessage)
            {
                this.loadingMessageContainer = new HtmlContainer();
                this.loadingMessageContainer.CssClass = this.grid.CssSettings.CSSLoadingMessage;
                this.loadingMessageContainer.Style["display"] = "none";
                HtmlContainer child = new HtmlContainer {
                    Text = GridEncodingHelper.XMLCdataEncode(this.grid.LocalizationSettings.LoadingText)
                };
                this.loadingMessageContainer.Controls.Add(child);
                this.Controls.Add(this.loadingMessageContainer);
            }
        }

        private void CreateDataRow(object dataItem)
        {
            DataControlRowState rowState = (((this.rowIndex - this.customDataRowsNumber) % 2) == 0) ? DataControlRowState.Normal : DataControlRowState.Alternate;
            this.dataRow = new GridRow(this.rowIndex, this.rowIndex + (this.grid.GetPageSize(true) * this.grid.CurrentPageIndex), GridRowType.DataRow, rowState);
            if (!string.IsNullOrEmpty(this.recordStyleDisplay))
            {
                this.dataRow.Style["display"] = this.recordStyleDisplay;
            }
            this.dataRow.TableSection = TableRowSection.TableBody;
            this.dataRow.CssClass = (this.dataRow.RowState == DataControlRowState.Normal) ? this.grid.CssSettings.CSSRecord : this.grid.CssSettings.CSSRecordAlternate;
            this.detailsExpanderAdded = false;
            if (this.grid.GenerateRecordIds)
            {
                this.dataRow.ID = this.CreateDataRowID(this.dataRow);
            }
            if (!string.IsNullOrEmpty(this.grid.ClientSideEvents.OnClientDblClick))
            {
                this.dataRow.Attributes["ondblclick"] = this.grid.ClientSideEvents.OnClientDblClick + "(" + (this.grid.ClientSideEvents.ExposeSender ? (this.grid.GridID + ", ") : "") + this.rowIndex.ToString() + ")";
            }
            foreach (Column column in this.grid.Columns)
            {
                new GridDataControlFieldCell(column);
                this.CreateCell(column);
            }
            GridRowEventArgs e = new GridRowEventArgs(this.dataRow);
            this.grid.OnRowCreated(e);
            this.grid.dataRows.Add(this.dataRow);
            this.container.Rows.Add(this.dataRow);
            if (this.grid.internalDataBinding)
            {
                this.dataRow.DataItem = dataItem;
                this.dataRow.DataBind();
                if ((this.grid.AllowRecordSelection && this.grid.KeepSelectedRecords) && ((this.grid.SelectedRecords != null) && this.grid.IsSelectedRecord(this.dataRow.ToHashtable())))
                {
                    if (!string.IsNullOrEmpty(this.grid.selectedRowsIndexes))
                    {
                        this.grid.selectedRowsIndexes = this.grid.selectedRowsIndexes + ",";
                    }
                    this.grid.selectedRowsIndexes = this.grid.selectedRowsIndexes + this.rowIndex;
                }
                if (((this.grid.DetailGrids.Count > 0) && (this.grid.MasterDetailSettings.LoadingMode == DetailGridLoadingMode.OnCallback)) && (!this.grid.MasterDetailSettings.ShowEmptyDetails && !string.IsNullOrEmpty(this.grid.MasterDetailSettings.DetailsCountField)))
                {
                    Hashtable hashtable = this.dataRow.ToHashtable();
                    bool flag = false;
                    int result = 0;
                    if (hashtable.ContainsKey(this.grid.MasterDetailSettings.DetailsCountField))
                    {
                        flag = int.TryParse(hashtable[this.grid.MasterDetailSettings.DetailsCountField].ToString(), out result);
                    }
                    if (flag && (result <= 0))
                    {
                        this.detailsExpander.Style["display"] = "none";
                    }
                }
                this.grid.OnRowDataBound(e);
            }
            this.dataRows.Add(this.dataRow);
        }

        protected virtual string CreateDataRowID(GridRow dataRow)
        {
            return string.Concat(new object[] { "ob_", this.grid.GridID, "_R_", dataRow.RowIndex });
        }

        protected virtual bool CreateDetailGrid()
        {
            if (this.grid.DetailGrids.Count > 0)
            {
                if ((this.grid.MasterDetailSettings.LoadingMode == DetailGridLoadingMode.OnLoad) || this.grid.isExporting)
                {
                    this.CreateDetailsRow();
                }
                else
                {
                    Vdc.Grid.Grid rootMasterGrid = this.grid.GetRootMasterGrid();
                    int num = -1;
                    if ((rootMasterGrid.ArrMasterRowsForDetailGridToBeLoaded == null) || (rootMasterGrid.ArrMasterRowsForDetailGridToBeLoaded.Count <= 0))
                    {
                        this.CreateDetailRowContainer();
                    }
                    else
                    {
                        Hashtable hashtable = (this.container.Rows[this.container.Rows.Count - 1] as GridRow).ToHashtable();
                        int levelInHierarchy = this.grid.GetLevelInHierarchy();
                        Hashtable hashtable2 = null;
                        if (rootMasterGrid.ArrMasterRowsForDetailGridToBeLoaded.Count > levelInHierarchy)
                        {
                            hashtable2 = rootMasterGrid.ArrMasterRowsForDetailGridToBeLoaded[levelInHierarchy] as Hashtable;
                        }
                        if (hashtable2 == null)
                        {
                            this.CreateDetailRowContainer();
                        }
                        else
                        {
                            num++;
                            bool flag = true;
                            foreach (DictionaryEntry entry in hashtable)
                            {
                                if ((hashtable2.ContainsKey(entry.Key.ToString()) && (hashtable2[entry.Key.ToString()] != null)) && (entry.Value.ToString() != hashtable2[entry.Key.ToString()].ToString()))
                                {
                                    flag = false;
                                    string str = entry.Value.ToString();
                                    string str2 = hashtable2[entry.Key.ToString()].ToString();
                                    if (str.Replace(" ", "") == str2.Replace(" ", ""))
                                    {
                                        flag = true;
                                    }
                                    else if (!string.IsNullOrEmpty(this.grid.Columns[entry.Key.ToString()].DataFormatString) && this.grid.Columns[entry.Key.ToString()].ApplyFormatInEditMode)
                                    {
                                        flag = true;
                                    }
                                    if (!flag)
                                    {
                                        break;
                                    }
                                }
                            }
                            if (flag)
                            {
                                this.CreateDetailsRow();
                                return this.grid.CallbackMode;
                            }
                            this.CreateDetailRowContainer();
                        }
                    }
                }
            }
            return false;
        }

        protected virtual void CreateDetailRowContainer()
        {
            GridRow row = new GridRow(-1, -1, GridRowType.EmptyDataRow, DataControlRowState.Normal);
            row.Style["display"] = "none";
            TableCell cell = new TableCell {
                ColumnSpan = this.grid.Columns.Count
            };
            row.Cells.Add(cell);
            this.container.Rows.Add(row);
            this.customDataRowsNumber++;
            this.rowIndex++;
        }

        protected virtual void CreateDetailsRow()
        {
            if (this.grid.AvailableDetailGrids == null)
            {
                this.grid.AvailableDetailGrids = new Hashtable();
            }
            GridRow row = new GridRow(-1, -1, GridRowType.Detail, DataControlRowState.Normal) {
                CssClass = this.grid.CssSettings.CSSDetailGridContainer
            };
            this.customDataRowsNumber++;
            if ((this.grid.MasterDetailSettings.State == DetailGridState.Collapsed) && this.grid.CallbackMode)
            {
                row.Style["display"] = "none";
            }
            else if (!this.grid.CallbackMode)
            {
                GridRow row2 = this.dataRows[this.dataRows.Count - 1];
                foreach (GridDataControlFieldCell cell in row2.Controls)
                {
                    int count = cell.Controls[0].Controls.Count;
                    if (cell.Visible && (cell.CssClass.IndexOf("ob_gDGE") != -1))
                    {
                        Image image = cell.Controls[0].Controls[cell.Controls[0].Controls.Count - 2].Controls[0] as Image;
                        image.ImageUrl = this.grid.ImagesURL.Details_BtnClose;
                        break;
                    }
                }
            }
            TableCell cell2 = new TableCell {
                ColumnSpan = this.grid.Columns.Count
            };
            row.Cells.Add(cell2);
            HtmlContainer child = new HtmlContainer {
                CssClass = "ob_gDGCCT"
            };
            cell2.Controls.Add(child);
            HtmlContainer container2 = new HtmlContainer {
                CssClass = "ob_gDGCCIT"
            };
            child.Controls.Add(container2);
            this.container.Rows.Add(row);
            for (int i = 0; i < this.grid.DetailGrids.Count; i++)
            {
                this.grid.DetailGrids[i].MasterRow = this.dataRows[this.dataRows.Count - 1];
                this.grid.DetailGrids[i].MasterGrid = this.grid;
                this.grid.DetailGrids[i].MasterRow.ToHashtable();
                DetailGrid grid = this.grid.DetailGrids[i].Clone() as DetailGrid;
                if (!this.grid.AvailableDetailGrids.ContainsKey(grid.ID))
                {
                    this.grid.AvailableDetailGrids.Add(grid.ID, grid);
                }
                else
                {
                    object dataSource = null;
                    if (string.IsNullOrEmpty(grid.DataSourceID) && (grid.DataSource != null))
                    {
                        dataSource = grid.DataSource;
                    }
                    grid = this.grid.AvailableDetailGrids[grid.ID] as DetailGrid;
                    if (dataSource != null)
                    {
                        grid.DataSource = dataSource;
                    }
                }
                grid.MasterRow = this.dataRows[this.dataRows.Count - 1];
                grid.MasterGrid = this.grid;
                grid.CopyUnchangeableProperties(this.grid.DetailGrids[i]);
                this.grid.DetailGrids[i].ConcreteInstances.Add(grid);
                grid.isExporting = this.grid.isExporting;
                if (this.grid.MasterDetailSettings.LoadingMode == DetailGridLoadingMode.OnCallback)
                {
                    grid.clearDataSourceSelectArgumentsSortExpression = true;
                }
                container2.Controls.Add(grid);
                Vdc.Grid.Grid rootMasterGrid = this.grid.GetRootMasterGrid();
                if (this.grid.MasterDetailSettings.LoadingMode == DetailGridLoadingMode.OnCallback)
                {
                    if ((rootMasterGrid.RootGridPostDataKey != null) && (rootMasterGrid.RootGridPostCollection != null))
                    {
                        grid.LoadPostData(rootMasterGrid.RootGridPostDataKey, rootMasterGrid.RootGridPostCollection);
                        grid.RaisePostDataChangedEvent();
                        grid.DataBind();
                    }
                    if (string.IsNullOrEmpty(grid.DataSourceID) && (grid.DataSource != null))
                    {
                        grid.DataBind();
                    }
                }
                else
                {
                    if ((rootMasterGrid.RootGridPostDataKey != null) && (rootMasterGrid.RootGridPostCollection != null))
                    {
                        grid.LoadPostData(rootMasterGrid.RootGridPostDataKey, rootMasterGrid.RootGridPostCollection);
                        grid.RaisePostDataChangedEvent();
                        grid.DataBind();
                    }
                    if (grid.IsPostBack || (string.IsNullOrEmpty(grid.DataSourceID) && (grid.DataSource != null)))
                    {
                        grid.DataBind();
                    }
                }
            }
            this.rowIndex++;
        }

        private void CreateEmptyDataRow()
        {
            GridRow row = new GridRow(-1, -1, GridRowType.EmptyDataRow, DataControlRowState.Normal) {
                CssClass = this.grid.CssSettings.CSSRecord,
                TableSection = TableRowSection.TableBody
            };
            TableCell child = new TableCell {
                ColumnSpan = this.grid.Columns.Count,
                CssClass = this.grid.CssSettings.CSSNoRecordsMessage
            };
            child.Attributes["align"] = "center";
            HtmlContainer container = new HtmlContainer {
                Text = GridEncodingHelper.XMLCdataEncode(this.grid.LocalizationSettings.NoRecordsText),
                CssClass = "ob_gCc1"
            };
            child.Controls.Add(container);
            row.Controls.Add(child);
            GridRowEventArgs e = new GridRowEventArgs(row);
            this.grid.OnRowCreated(e);
            this.container.Rows.Add(row);
            this.dataRows.Add(row);
        }

        protected virtual void CreateExpandCollapseButtonContainer(GridDataControlFieldCell cell)
        {
            cell.CssClass = "ob_gDGE " + cell.CssClass;
            HtmlContainer child = new HtmlContainer {
                CssClass = "ob_gDGEB"
            };
            Image image = new Image();
            if (this.grid.MasterDetailSettings.State == DetailGridState.Collapsed)
            {
                image.ImageUrl = this.grid.ImagesURL.Details_BtnOpen;
            }
            else
            {
                image.ImageUrl = this.grid.ImagesURL.Details_BtnClose;
            }
            image.Attributes["alt"] = "";
            child.Controls.Add(image);
            cell.Controls[0].Controls.Add(child);
            this.detailsExpander = child;
        }

        protected virtual void CreateGroupFooter(int level)
        {
            GridRow row = new GridRow(0, 0, GridRowType.GroupFooter, DataControlRowState.Normal) {
                CssClass = this.grid.CssSettings.CSSRecordGroupFooter,
                GroupLevel = level,
                TableSection = TableRowSection.TableBody
            };
            if (this.grid.IsDesignTimeRender && this.grid.UseEmbeddedImages)
            {
                row.Style["background-image"] = "url(" + this.grid.ImagesURL.GroupFooter + ")";
            }
            if (this.grid.ShowCollapsedGroups)
            {
                row.Style["display"] = "none";
            }
            foreach (Column column in this.grid.Columns)
            {
                new GridDataControlFieldCell(column);
                row.Controls.Add(this.CreateGroupFooterCell(column));
            }
            GridRowEventArgs e = new GridRowEventArgs(row);
            this.grid.OnRowCreated(e);
            if (this.grid.internalDataBinding)
            {
                this.grid.OnRowDataBound(e);
            }
            this.container.Rows.Add(row);
            this.rowIndex++;
            this.customDataRowsNumber++;
        }

        protected virtual GridDataControlFieldCell CreateGroupFooterCell(Column field)
        {
            GridDataControlFieldCell cell = new GridDataControlFieldCell(field);
            if (field.Visible)
            {
                cell.CssClass = !field.Wrap ? "ob_gC" : "ob_gCW";
            }
            else
            {
                cell.CssClass = !field.Wrap ? "ob_gC_I" : "ob_gCW_I";
            }
            if (field.Index == this.grid.IndexOfFirstVisibleColumn)
            {
                cell.CssClass = cell.CssClass + " ob_gC_Fc";
            }
            else if (field.Index == this.grid.IndexOfLastVisibleColumn)
            {
                cell.CssClass = cell.CssClass + " ob_gC_Lc";
            }
            if (field.Width.IndexOf("%") == -1)
            {
                cell.Width = Unit.Pixel(int.Parse(field.Width));
            }
            else
            {
                cell.Width = Unit.Percentage((double) int.Parse(field.Width.Replace("%", "")));
            }
            if (this.grid.IsDesignTimeRender)
            {
                if (!field.Visible)
                {
                    cell.Style["display"] = "none";
                    cell.Width = Unit.Pixel(0);
                }
                cell.Attributes["_designerRegion"] = field.Index.ToString();
            }
            HtmlContainer child = new HtmlContainer();
            HtmlContainer container2 = new HtmlContainer();
            child.CssClass = "ob_gCc1";
            if (string.IsNullOrEmpty(field.Align) || (field.Align.ToLower() == "left"))
            {
                container2.CssClass = "ob_gCc2";
            }
            else if (field.Align.ToLower() == "center")
            {
                container2.CssClass = "ob_gCc2C";
            }
            else
            {
                container2.CssClass = "ob_gCc2R";
            }
            child.Controls.Add(container2);
            cell.Controls.Add(child);
            return cell;
        }

        protected virtual void CreateTableLayoutRow()
        {
            TableRow row = new TableRow {
                CssClass = "ob_gBTLRV",
                TableSection = TableRowSection.TableHeader
            };
            foreach (Column column in this.grid.Columns)
            {
                TableCell cell = new TableCell {
                    CssClass = "ob_gC"
                };
                if (column.Visible)
                {
                    cell.CssClass = !column.Wrap ? "ob_gC" : "ob_gCW";
                }
                else
                {
                    cell.CssClass = !column.Wrap ? "ob_gC_I" : "ob_gCW_I";
                }
                if (column.Width.IndexOf("%") == -1)
                {
                    cell.Width = Unit.Pixel(int.Parse(column.Width));
                }
                else
                {
                    cell.Width = Unit.Percentage((double) int.Parse(column.Width.Replace("%", "")));
                }
                if (!column.Visible && this.grid.IsDesignTimeRender)
                {
                    cell.Style["display"] = "none";
                    cell.Width = Unit.Pixel(0);
                }
                row.Cells.Add(cell);
            }
            this.container.Rows.Add(row);
        }

        internal virtual void Export(Control container)
        {
            int num = 0;
            for (int i = 1; i < this.innerBody.Controls[0].Controls.Count; i++)
            {
                TableRow child = new TableRow();
                GridRow sourceRow = this.innerBody.Controls[0].Controls[i] as GridRow;
                bool flag = false;
                if (sourceRow.RowType == GridRowType.DataRow)
                {
                    bool flag2 = false;
                    if (num == 0)
                    {
                        flag2 = true;
                    }
                    foreach (int num3 in this.grid.columnIndexesToExport)
                    {
                        TableCell cell = new TableCell();
                        if (this.grid.ExportingSettings.KeepColumnSettings)
                        {
                            cell.Attributes["width"] = this.grid.Columns[num3].Width;
                            cell.Attributes["align"] = this.grid.Columns[num3].Align;
                            if (!this.grid.Columns[num3].Wrap)
                            {
                                cell.Attributes["nowrap"] = "nowrap";
                            }
                        }
                        if (!string.IsNullOrEmpty(this.grid.CssSettings.CSSExportCellStyle))
                        {
                            cell.Attributes["style"] = this.grid.CssSettings.CSSExportCellStyle;
                        }
                        if (this.grid.ExportingSettings.ExportTemplates && !string.IsNullOrEmpty(this.grid.Columns[num3].TemplateSettings.TemplateId))
                        {
                            StringWriter writer = new StringWriter();
                            Html32TextWriter writer2 = new Html32TextWriter(writer);
                            ((GridDataControlFieldCell) sourceRow.Controls[num3]).Controls[0].Controls[0].RenderControl(writer2);
                            cell.Text = writer.ToString();
                        }
                        else
                        {
                            cell.Text = ((GridDataControlFieldCell) sourceRow.Controls[num3]).GetInnerText();
                            if (this.grid.Columns[num3].ExportAsText)
                            {
                                cell.Text = "&nbsp;" + cell.Text;
                            }
                        }
                        child.Controls.Add(cell);
                        if (flag2)
                        {
                            num++;
                        }
                    }
                    flag = true;
                }
                else if ((sourceRow.RowType == GridRowType.GroupHeader) && this.grid.ExportingSettings.ExportGroupHeader)
                {
                    TableCell cell2 = new TableCell();
                    TableCell cell3 = (TableCell) sourceRow.Controls[0];
                    cell2.ColumnSpan = cell3.ColumnSpan;
                    cell2.Text = ((Literal) cell3.Controls[0].Controls[cell3.Controls[0].Controls.Count - 1].Controls[0]).Text;
                    child.Controls.Add(cell2);
                    flag = true;
                }
                else if ((sourceRow.RowType == GridRowType.GroupFooter) && this.grid.ExportingSettings.ExportGroupFooter)
                {
                    foreach (int num4 in this.grid.columnIndexesToExport)
                    {
                        TableCell cell4 = new TableCell();
                        if (this.grid.ExportingSettings.KeepColumnSettings)
                        {
                            cell4.Attributes["width"] = this.grid.Columns[num4].Width;
                            cell4.Attributes["align"] = this.grid.Columns[num4].HeaderAlign;
                            if (!this.grid.Columns[num4].Wrap)
                            {
                                cell4.Attributes["nowrap"] = "nowrap";
                            }
                        }
                        cell4.Text = ((GridDataControlFieldCell) sourceRow.Controls[num4]).Text;
                        if (this.grid.Columns[num4].ExportAsText)
                        {
                            cell4.Text = "&nbsp;" + cell4.Text;
                        }
                        child.Controls.Add(cell4);
                    }
                    flag = true;
                }
                else if ((sourceRow.RowType == GridRowType.Detail) && this.grid.ExportingSettings.ExportDetails)
                {
                    TableCell cell5 = new TableCell();
                    TableCell cell1 = (TableCell) sourceRow.Controls[0];
                    cell5.ColumnSpan = num;
                    child.Controls.Add(cell5);
                    Control control = sourceRow.Controls[0].Controls[0].Controls[0];
                    foreach (Control control2 in control.Controls)
                    {
                        ((DetailGrid) control2).Export(null, cell5);
                    }
                    flag = true;
                }
                if (flag)
                {
                    this.grid.exportedGrid.Controls.Add(child);
                    this.grid.OnRowExported(child, sourceRow);
                }
            }
        }

        internal void ForceEnsureChildControls()
        {
            this.EnsureChildControls();
        }

        protected virtual string GetBodyID()
        {
            return ("ob_" + this.grid.GridID + "BodyContainer");
        }

        protected virtual object GetValue(GridDataControlFieldCell cell)
        {
            Column containingField = cell.ContainingField as Column;
            object cellValue = null;
            try
            {
                cellValue = containingField.GetCellValue(cell.Parent);
            }
            catch (Exception exception1)
            {
                if (exception1 != null)
                {
                }
                cellValue = string.Empty;
            }
            if (string.IsNullOrEmpty(containingField.NullDisplayText) || ((cellValue != DBNull.Value) && (!string.IsNullOrEmpty(cellValue as string) || !containingField.ConvertEmptyStringToNull)))
            {
                return cellValue;
            }
            return containingField.NullDisplayText;
        }

        protected virtual void PopulateCell(object sender, EventArgs e)
        {
            GridDataControlFieldCell cell = sender as GridDataControlFieldCell;
            Column containingField = cell.ContainingField as Column;
            HtmlContainer container = cell.Controls[0].Controls[0] as HtmlContainer;
            if (!(containingField is CheckBoxSelectColumn))
            {
                if (string.IsNullOrEmpty(containingField.DataField))
                {
                    if (!containingField.AllowEdit && !containingField.AllowDelete)
                    {
                        container.Text = "";
                    }
                    if (!string.IsNullOrEmpty(containingField.TemplateSettings.TemplateId))
                    {
                        this.BindCellTemplate(cell, null);
                    }
                }
                else
                {
                    object obj2 = this.GetValue(cell);
                    string str = GridEncodingHelper.XMLCdataEncode(obj2.ToString(), true);
                    if (!string.IsNullOrEmpty(containingField.DataFormatString))
                    {
                        obj2 = string.Format(CultureInfo.CurrentCulture, containingField.DataFormatString, new object[] { obj2 });
                    }
                    string dataValue = obj2.ToString();
                    if (!containingField.ParseHTML)
                    {
                        dataValue = GridEncodingHelper.XMLCdataEncode(obj2.ToString(), true);
                    }
                    dataValue = dataValue.Replace("\r\n", "<br />");
                    if ((dataValue == string.Empty) && this.grid.IsDesignTimeRender)
                    {
                        dataValue = "&#160;";
                    }
                    if (cell.Controls[0].Controls.Count > 1)
                    {
                        HtmlContainer container2 = cell.Controls[0].Controls[cell.Controls[0].Controls.Count - 1] as HtmlContainer;
                        bool flag = true;
                        if (((!containingField.AllowEdit && !containingField.AllowDelete) && (!string.IsNullOrEmpty(containingField.DataField) && !string.IsNullOrEmpty(containingField.DataFormatString))) && containingField.ApplyFormatInEditMode)
                        {
                            flag = false;
                        }
                        container2.Text = flag ? str : dataValue;
                    }
                    if (!string.IsNullOrEmpty(containingField.TemplateSettings.TemplateId))
                    {
                        this.BindCellTemplate(cell, dataValue);
                    }
                    else if (!(containingField is CheckBoxColumn))
                    {
                        container.Text = dataValue;
                    }
                    else if (containingField is CheckBoxColumn)
                    {
                        CheckBox child = new CheckBox {
                            Enabled = false,
                            Checked = (obj2.ToString().ToLower() == "true") || (obj2.ToString() == "1")
                        };
                        container.Controls.Add(child);
                    }
                }
            }
            else if (containingField is CheckBoxSelectColumn)
            {
                CheckBoxSelectColumn column2 = containingField as CheckBoxSelectColumn;
                if (column2.ControlType == GridControlType.Standard)
                {
                    CheckBox box2 = new CheckBox();
                    container.Controls.Add(box2);
                }
                else
                {
                    string str3 = "ob_chkSel" + this.grid.GridID + "_" + containingField.Index.ToString() + "_" + this.rowIndex.ToString();
                    HiddenField field = new HiddenField {
                        Value = str3
                    };
                    container.Controls.Add(field);
                    VdcCheckBox box3 = new VdcCheckBox
                    {
                        ID = str3
                    };
                    container.Controls.Add(box3);
                }
            }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }
    }
}

